<?php

/**
 * @author Martin Pernica
 * @copyright 2008
 */
 echo '<br /><h2><a href="index.php?p=components&name=content">'.Lang::getBackGlobal('back').'</a></h2><br />';
 
if(!empty($_POST['title']) && (!empty($_POST['pretext']) || !empty($_POST['text'])) && !empty($_POST['cat_id'])){

	$title = Registry::get('db')->real_escape_string(strip_tags($_POST['title']));
	$pretext = Registry::get('db')->real_escape_string($_POST['pretext']);
    $text = Registry::get('db')->real_escape_string($_POST['text']);
	$tags = strip_tags(Registry::get('db')->real_escape_string($_POST['tags']));
	$cat_id = intval($_POST['cat_id']);
	$closed = (intval($_POST['closed'] == 1)) ? '1' : '0';
	$published = (intval($_POST['published'] == 1)) ? '1' : '0';
	$frontpage = (intval($_POST['frontpage'] == 1)) ? '1' : '0';
	$notice = (intval($_POST['notice'] == 1)) ? '1' : '0';

	$query = Registry::get('db')->query("UPDATE {prefix}articles SET title = '".$title."', pretext = '".$pretext."', text = '".$text."', tags = '".$tags."', cat_id = '".$cat_id."', frontpage = '".$frontpage."', notice = '".$notice."', closed = '".$closed."', published = '".$published."' WHERE id = '".intval($_GET['id'])."'");
	if($query){
        $log->write(8, 'com_content', 'NOW()', 'User edited article #'.intval($_GET['id']), $_SESSION['id']);
        messageBox( Lang::getBack('com_content', 'edited_done') );
	}else{
         $log->write(1, 'com_content', 'NOW()', 'Error, failed to edit the article #'.intval($_GET['id']), $_SESSION['id']);
	}
		
}
$query = Registry::get('db')->query("SELECT * FROM {prefix}articles WHERE id = '".intval($_GET['id'])."'");
$data = $query->fetch_array();

$sections = Registry::get('db')->query("SELECT id, name FROM {prefix}sections");

$sections_select = array();

while ($sec = $sections->fetch_array()) {
    $categories = Registry::get('db')->query("SELECT id, name, section FROM {prefix}categories WHERE section = '" . $sec['id'] . "'");
    $sections_select[$sec['name']] = array();
    while ($cats = $categories->fetch_array()) {
        $sections_select[$sec['name']][$cats['name']] = $cats['id'];
    }
}

$closed = ($data['closed'] == 1) ? 1 : 0;
$published = ($data['published'] == 1) ? 1 : 0;
$frontpage = ($data['frontpage'] == 1) ? 1 : 0;
$notice = ($data['notice'] == 1) ? 1 : 0;

$form = new Form;
$form->addTextbox('title', Lang::getBack('com_content', 'title'), stripslashes($data['title']));
$form->addTextarea('pretext', Lang::getBack('com_content', 'pretext'), stripslashes($data['pretext']), 30, 80, 'wysiwyg');
$form->addTextarea('text', Lang::getBack('com_content', 'text'), stripslashes($data['text']), 30, 80, 'wysiwyg');
$form->addTextarea('tags', Lang::getBack('com_content', 'tags'), stripslashes($data['tags']), 5, 30);
$form->addSelect('cat_id', Lang::getBack('com_content', 'category'), $sections_select, $data['cat_id']);
$form->addCheckbox('frontpage', Lang::getBack('com_content', 'frontpage'), 1, $frontpage);
$form->addCheckbox('notice', Lang::getBack('com_content', 'notice'), 1, $notice);
$form->addCheckbox('closed', Lang::getBack('com_content', 'closed'), 1, $closed);
$form->addCheckbox('published', Lang::getBack('com_content', 'published'), 1, $published);
$form->addSubmit('submit_edit', Lang::getBackGlobal('submit'));

echo $form;

?>